#include "env.h"
#include <QtCore>
#include <QtGui>
#include <QtNetwork>
#include "t_core/nam.h"

Env *g_Env = 0;

Env::Env()
{
	g_Env = this;
	m_settings = new QSettings(settingsFileName(), QSettings::IniFormat, this);
	settingsLoad();
	settingsSave();
    nam = new BatchNetworkAccessManager(this);
    //nam->appendBackendFactory(&f_bef);
	mainWindow = new MainWindow(0);
	this->page1 = new T_Page1();
}

Env::~Env()
{
	settingsSave();
	////mainWindow->deleteLater();
}

QString Env::settingsFileName()
{
	QString v_dirPath = QDesktopServices::storageLocation(QDesktopServices::DesktopLocation);
	QDir v_dir(v_dirPath);
	QString v_filePath = v_dir.filePath("yt_dump.ini");
	qDebug() << "[Env::settingsFileName()]" << v_filePath;
	return v_filePath;
}

void Env::settingsLoad()
{
	m_settings->beginGroup("auth");
	this->youtube_login_id = m_settings->value("youtube_login_id").toString();
	this->youtube_password = m_settings->value("youtube_password").toString();
	this->youtube_devl_key = m_settings->value("youtube_devl_key").toString();
	this->youtube_auth_key = m_settings->value("youtube_auth_key").toString();
	m_settings->endGroup();
}

void Env::settingsSave()
{
	m_settings->beginGroup("auth");
	m_settings->setValue("youtube_login_id", this->youtube_login_id);
	m_settings->setValue("youtube_password", this->youtube_password);
	m_settings->setValue("youtube_devl_key", this->youtube_devl_key);
	m_settings->setValue("youtube_auth_key", this->youtube_auth_key);
	m_settings->endGroup();
	m_settings->sync();
}

void Env::youtubeLogin()
{
	QUrl v_url;
    v_url.setEncodedUrl("https://www.google.com/youtube/accounts/ClientLogin");
    //v_url.setEncodedUrl("https://www.google.com/accounts/ClientLogin");
	QUrl v_param;
	v_param.setEncodedUrl("?");
	v_param.addQueryItem("Email", this->youtube_login_id);
	v_param.addQueryItem("Passwd", this->youtube_password);
    v_param.addQueryItem("service", "youtube");
    v_param.addQueryItem("source", "yt_dump");
    qDebug() << "[v_param.toEncoded()]" << v_param.toEncoded();
	QByteArray v_post_data = v_param.toEncoded();
	v_post_data.remove(0, 1);
    qDebug() << "[v_post_data]" << v_post_data;

	QNetworkRequest v_request;
	v_request.setUrl(v_url);
	v_request.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
	QByteArray v_result = nam->doPost(v_request, v_post_data);
    qDebug() << "[v_result]" << v_result;

	v_result.replace('\n','&');
	v_result.prepend("?");
	QUrl v_auth_params;
	v_auth_params.setEncodedUrl(v_result);
    qDebug() << "[v_auth_params]" << v_auth_params;
    QString v_p_auth = v_auth_params.queryItemValue("Auth");
	QString v_p_user = v_auth_params.queryItemValue("YouTubeUser");
	QString v_p_error = v_auth_params.queryItemValue("Error");
	qDebug() << "[v_p_auth]" << v_p_auth;
	qDebug() << "[v_p_user]" << v_p_user;
	qDebug() << "[v_p_error]" << v_p_error;
    if(v_p_auth.isEmpty())
    {
        QMessageBox::information(0, "Login Failed", v_param.toString());
        QMessageBox::information(0, "Login Failed", v_p_error);
        QMessageBox::information(0, "Login Failed", v_result);
        QMessageBox::information(0, "Login Failed", v_auth_params.toString());
    }
	this->youtube_auth_key = v_p_auth;
	this->settingsSave();
}

void Env::showMessage(QString a_msg)
{
	this->mainWindow->statusBar()->showMessage(a_msg);
}
